package org.lq.his.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.lq.his.dao.SectionDao;
import org.lq.his.pojo.Section;
import org.lq.his.service.SectionService;
import org.lq.his.service.impl.SectionServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/section")
public class SectionController {
	@Resource
	private SectionService sectionService;
	
	public void setSectionService(SectionService sectionService) {
		this.sectionService = sectionService;
	}

	@RequestMapping("/index")
	public String index() throws Exception{
		return "section/sectionIndex";
	}
	
	@RequestMapping("/form")
	public String add() throws Exception{
		return "section/sectionForm";
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> sectionList(@RequestParam("page") Integer page,@RequestParam("rows") Integer rows,@RequestParam(defaultValue="id") String sort,@RequestParam(defaultValue="asc") String order,Section condition) throws Exception{
		Map<String, Object> map = new HashMap<>();
		int start = (page - 1) * rows;
		List<Section> list = sectionService.getListByCondition(start, rows, condition, sort, order);
		Integer total = sectionService.getCountByCondition(condition);
		map.put("rows", list);
		map.put("total", total);
		return map;
	}
	
	@RequestMapping("/insert")
	@ResponseBody
	public Map<String, Object> sectionAdd(Section section){
		Map<String, Object> map = new HashMap<>();
		sectionService.add(section);
		map.put("result", true);
		return map;
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Map<String, Object> sectionEdit(Section section){
		Map<String, Object> map = new HashMap<>();
		sectionService.update(section);
		map.put("result", true);
		return map;
	}
	
	@RequestMapping("/sid")
	@ResponseBody
	public Section sectionId(Integer id) throws Exception{
		return sectionService.getById(id);
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> delete(Integer[] ids) throws Exception{
		Map<String, Object> map = new HashMap<>();
		sectionService.deleteByIds(ids);
		map.put("result", true);
		return map;
	}
	
	@RequestMapping("/editstate")
	@ResponseBody
	public Map<String, Object> EditState(Section section){
		Map<String, Object> map = new HashMap<>();
		sectionService.editState(section);
		map.put("result", true);
		return map;
	}
	
	@RequestMapping("/selectAll")
	@ResponseBody
	public List<Section> selectAll() throws Exception {
		
		return sectionService.getAll();
	}
}
